package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.UploadTrackVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	@Operation(summary = "上传音频文件")
	@PostMapping("/uploadTrack")
	public Result<UploadTrackVo> uploadTrack(MultipartFile file) {
		UploadTrackVo uploadTrackVo = vodService.uploadTrack(file);
		return Result.ok(uploadTrackVo);
	}

	@Operation(summary = "保存音频信息")
	@PostMapping("/saveTrackInfo")
	@GuiGuLogin
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.saveTrackInfo(trackInfoVo, AuthContextHolder.getUserId());
		return Result.ok();
	}

	@Operation(summary = "分页查询用户音频信息")
	@PostMapping("/findUserTrackPage/{page}/{limit}")
	@GuiGuLogin
	public Result<IPage<TrackListVo>> findUserTrackPage(
			@Parameter(description = "当前页",name = "page",required = true)
			@PathVariable Long page,
			@Parameter(description = "每页数量",name = "limit",required = true)
			@PathVariable Long limit,
			@Parameter(description = "查询对象",name = "trackInfoQuery",required = false)
			@RequestBody TrackInfoQuery trackInfoQuery
	){
		trackInfoQuery.setUserId(AuthContextHolder.getUserId());
		IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(page,limit,trackInfoQuery);
		return Result.ok(trackListVoIPage);
	}

	/**
	 * 根据id获取音频信息
	 *
	 * @param id 音频的ID
	 * @return 包含音频信息的Result对象，如果成功则返回包含音频信息的Result对象，如果失败则返回错误信息
	 */
	@Operation(summary = "根据id获取音频信息")
	@GetMapping("/getTrackInfo/{id}")
	@GuiGuLogin
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	@Operation(summary = "根据id更新音频信息")
	@PutMapping("/updateTrackInfo/{id}")
	@GuiGuLogin
	public Result updateTrackInfo(@PathVariable Long id, @RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(trackInfoVo, id);
		return Result.ok();
	}

	@Operation(summary = "根据id删除音频信息")
	@DeleteMapping("/removeTrackInfo/{id}")
	@GuiGuLogin
	public Result removeTrackInfo(@PathVariable Long id) {
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

}

